
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 范邦振
 * @version 0.0.1
 * @see <a href="https://www.zhihu.com/question/39651928">PO、VO、DTO使用说明</a>
 * @since 2020-07-17 13:43
 */
public class SearchUtils {

    public static List<String> readfile(String filepath) throws FileNotFoundException, IOException {
        List<String> strings = new ArrayList<>();
        try {
            File file = new File(filepath);
            if (file.isDirectory()) {
                List<String> filelist = Arrays.asList(file.list());
                System.out.println("求取结果" + filelist);

                for (String s : filelist) {
                    File readfile = new File(filepath + "//" + s);
                    if (!readfile.isDirectory()) {
                        System.out.println("path=" + readfile.getPath());
                        System.out.println("absolutepath=" + readfile.getAbsolutePath());
                        System.out.println("name=" + readfile.getName());
                        strings.add(readfile.getPath());

                    } else if (readfile.isDirectory()) {
                        System.out.println("新的地址是" + filepath + "//" + s);
                        strings.addAll(readfile(filepath + "//" + s));
                    }
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("readfile()   Exception:" + e.getMessage());
        } finally {
            return strings;
        }
    }

    public static void allTransform(String filePath, String o, String n) throws FileNotFoundException, IOException {
        List<String> strs = readfile(filePath);

        System.out.println(strs);
        for (String s : strs) {
            transform(s, o, n);
        }
    }

    /*
     ** param1 新的内容 param2 新的filePath param3 新的文件名
     */
    public static void SearchMethod1(String newContent, String newFilePath) throws IOException {

        File file = new File(newFilePath);

        File testPath = new File(file.getParent());
        testPath.delete();
        testPath.mkdirs();
        file.createNewFile();

        FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fileWriter);
        bw.write(newContent);
        bw.close();
    }

    public static void transform(String fileString, String O, String N) throws IOException {
        String o = toU(O);
        String n = toU(N);
        String newPath = fileString.replace(O, N).replace(o, n).replace("model2", "target");
        String newContent = GetContent(fileString).replace(O, N).replace(o, n);
        SearchMethod1(newContent, newPath);
        // write2(fileString, newPath, O, o, N, n);
    }

    public static String toU(String O) {
        String o = O.substring(0, 1).toUpperCase() + O.substring(1);
        return o;
    }

    public static String GetContent(String fileString) throws IOException {
        File file = new File(fileString);
        FileReader reader = new FileReader(file);
        BufferedReader bReader = new BufferedReader(reader);
        StringBuilder sb = new StringBuilder();
        String s = "";
        while ((s = bReader.readLine()) != null) {
            sb.append(s + "\n");
        }
        bReader.close();
        String str = sb.toString();
        return str;
    }

}
